package com.mvp.shq.washer.ui.orderDetail;

import com.mvp.shq.washer.entity.OrderInfo;
import com.mvp.shq.washer.entity.result.BaseResult;
import com.mvp.shq.washer.http.BaseObserver;
import com.mvp.shq.washer.utils.SpUtil;
import com.mvp.shq.washer.utils.T;

/**
 * Created by Administrator on 2017/8/28.
 */

public class OrderDetailPresenter extends OrderDetailConstract.Presenter {
    @Override
    public void onStart() {

    }

    @Override
    public void getOrders(String orderNo) {
        String token = SpUtil.getToken();
        model.getOrderInfo(token,orderNo)
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .subscribe(new BaseObserver<OrderInfo>(view) {
                    @Override
                    protected void onSuccees(BaseResult<OrderInfo> t) throws Exception {
                        if (t.isError()){
                            view.getOrderF(t.getMsg());
                        }else {
                            view.getOrderS(t.getData());
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        e.printStackTrace();
                        view.getOrderF("未知错误");
                    }
                });
    }

    @Override
    public void setErrorOrder(String orderNo) {
        String token = SpUtil.getToken();
        model.setErrorOrder(token,orderNo)
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .subscribe(new BaseObserver<String>(view) {
                    @Override
                    protected void onSuccees(BaseResult<String> t) throws Exception {
                        if (t.isError()){
                            T.show(t.getMsg());
                        }else {
                            view.setErrorOrderS();
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        T.show(e.getMessage());
                    }
                });
    }

    @Override
    public void refund(String orderNo, String content) {
        String token = SpUtil.getToken();
        String userId = SpUtil.getUser().getId();
        model.refund(token,userId,orderNo,content)
                .compose(getFragmentLifecycleProvider().bindToLifecycle())
                .subscribe(new BaseObserver<String>(view) {
                    @Override
                    protected void onSuccees(BaseResult<String> t) throws Exception {
                        if (t.isError()){
                            T.show(t.getMsg());
                        }else {
                            view.refundS();
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        T.show(e.getMessage());
                    }
                });
    }
}